package com.hongkzh.www.look.media.view.adapter;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.hongkzh.www.R;
import com.hongkzh.www.look.media.model.bean.CarouselBean;
import com.hongkzh.www.look.media.model.bean.Top10Bean;
import com.hongkzh.www.look.media.model.bean.UserCategoryBean;
import com.hongkzh.www.look.media.view.framgent.MedPopFragment;
import com.hongkzh.www.other.banner.GlideImageLoader;
import com.hongkzh.www.other.glide.GlideLoadUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asad on 2018/3/27.
 * 自媒体广告 adapter
 */

public class MedRvAdapter extends RecyclerView.Adapter implements View.OnClickListener, MedCarRvAdapter.OnClassifyClickListener, MedTopRvAdapter.OnItemClickListener {
    private final int CAROUSEL = 0;
    private final int TOP10 = 1;
    private final int ITEM = 0;
    private final int ITEM1 = 1;
    private final int ITEM2 = 2;
    private final Activity activity;
    private final FragmentManager supportFragmentManager;
    private List<CarouselBean.DataBean> data = new ArrayList<>();
    private List<String> imgSrc = new ArrayList<>();
    List<Fragment> fragments = new ArrayList<>();
    private MedCarRvAdapter medCarRvAdapter = new MedCarRvAdapter();
    private MedTopRvAdapter medTopRvAdapter = new MedTopRvAdapter();
    private int mposition;
    private RadioGroup medRg;

    public MedRvAdapter(Activity activity, RadioGroup medRg, FragmentManager supportFragmentManager) {
        this.activity = activity;
        this.medRg = medRg;
        this.supportFragmentManager = supportFragmentManager;
        fragments.add(new MedPopFragment());
        fragments.add(new MedPopFragment());
        fragments.add(new MedPopFragment());
    }

    @Override
    public int getItemViewType(int position) {
        if (position == CAROUSEL)
            return ITEM;
        if (position == TOP10)
            return ITEM1;
        return ITEM2;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.med_carousel, parent, false);
            return new ViewHolder(view);
        } else if (viewType == ITEM1) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.med_top10, parent, false);
            return new ViewHolder1(view);
        } else {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.med_footer, parent, false);
            return new ViewHolder2(view);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ITEM:
                final ViewHolder viewHolder = (ViewHolder) holder;
                if (data.size() != 0) {
                    GlideLoadUtils.getInstance().glideLoad(activity, data.get(0).getBackground(), viewHolder.medcarIma1);
                    GlideLoadUtils.getInstance().glideLoad(activity, data.get(0).getBackground(), viewHolder.medcarIma2);
                }

                viewHolder.medcarBan.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        if (position == data.size() + 1) position = 1;
                        if (position == 0) position = data.size();
                        if (mposition == position) return;
                        GlideLoadUtils.getInstance().glideLoad(activity, data.get(position - 1).getBackground(), viewHolder.medcarIma2);
                        ObjectAnimator.ofFloat(viewHolder.medcarIma2, "alpha", 0, 1)
                                .setDuration(800)
                                .start();
                        GlideLoadUtils.getInstance().glideLoad(activity, data.get(position - 1).getBackground(), viewHolder.medcarIma1);
                        mposition = position;
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                viewHolder.medcarBan.setIndicatorGravity(BannerConfig.RIGHT)
                        .setImageLoader(new GlideImageLoader())
                        .setImages(imgSrc)
                        .start();
                View view = LayoutInflater.from(activity).inflate(R.layout.item_medvf, null);
                View view1 = LayoutInflater.from(activity).inflate(R.layout.item_medvf, null);
                TextView viewById = view1.findViewById(R.id.neirong);
                viewById.setText("dsdsdsdsdsd");
                viewHolder.medcarVf.addView(view);
                viewHolder.medcarVf.addView(view1);
                viewHolder.medcarVf.startFlipping();

                medCarRvAdapter.setOnClassifyClickListener(this);
                viewHolder.medcarRv.setLayoutManager(new GridLayoutManager(viewHolder.itemView.getContext(), 5, LinearLayoutManager.VERTICAL, false));
                viewHolder.medcarRv.setAdapter(medCarRvAdapter);
                break;
            case ITEM1:
                ViewHolder1 viewHolder1 = (ViewHolder1) holder;
                medTopRvAdapter.setOnItemClickListener(this);
                viewHolder1.medtopRv.setLayoutManager(new LinearLayoutManager(viewHolder1.itemView.getContext(), LinearLayoutManager.HORIZONTAL, false));
                viewHolder1.medtopRv.setAdapter(medTopRvAdapter);
                break;
            case ITEM2:
                final ViewHolder2 viewHolder2 = (ViewHolder2) holder;
                RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (checkedId) {
                            case R.id.med_pop:
                                viewHolder2.medfotRg.check(R.id.medfot_pop);
                            case R.id.medfot_pop:
                                medRg.check(R.id.med_pop);
                                //点击第一个radiobutton,显示viewpager的第一页
                                viewHolder2.medfotVp.setCurrentItem(ITEM, false);
                                break;
                            case R.id.med_near:
                                viewHolder2.medfotRg.check(R.id.medfot_near);
                            case R.id.medfot_near:
                                medRg.check(R.id.med_near);
                                //点击第二个radiobutton,显示viewpager的第二页
                                viewHolder2.medfotVp.setCurrentItem(ITEM1, false);
                                break;
                            case R.id.med_cir:
                                viewHolder2.medfotRg.check(R.id.medfot_cir);
                            case R.id.medfot_cir:
                                medRg.check(R.id.med_cir);
                                //点击第二个radiobutton,显示viewpager的第三页
                                viewHolder2.medfotVp.setCurrentItem(ITEM2, false);
                                break;
                        }
                    }
                };
                medRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (checkedId) {
                            case R.id.med_pop:
                                viewHolder2.medfotPop.setChecked(true);
                                //点击第一个radiobutton,显示viewpager的第一页
                                viewHolder2.medfotVp.setCurrentItem(ITEM, false);
                                break;
                            case R.id.med_near:
                                viewHolder2.medfotNear.setChecked(true);
                                //点击第二个radiobutton,显示viewpager的第二页
                                viewHolder2.medfotVp.setCurrentItem(ITEM1, false);
                                break;
                            case R.id.med_cir:
                                viewHolder2.medfotCir.setChecked(true);
                                //点击第二个radiobutton,显示viewpager的第三页
                                viewHolder2.medfotVp.setCurrentItem(ITEM2, false);
                                break;
                        }
                    }
                });
                viewHolder2.medfotRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (checkedId) {
                            case R.id.medfot_pop:
                                medRg.check(R.id.med_pop);
                                //点击第一个radiobutton,显示viewpager的第一页
                                viewHolder2.medfotVp.setCurrentItem(ITEM, false);
                                break;
                            case R.id.medfot_near:
                                medRg.check(R.id.med_near);
                                //点击第二个radiobutton,显示viewpager的第二页
                                viewHolder2.medfotVp.setCurrentItem(ITEM1, false);
                                break;
                            case R.id.medfot_cir:
                                medRg.check(R.id.med_cir);
                                //点击第二个radiobutton,显示viewpager的第三页
                                viewHolder2.medfotVp.setCurrentItem(ITEM2, false);
                                break;
                        }
                    }
                });
                //viewpager滑动监听
                viewHolder2.medfotVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        //radiogroup选中对应的radiobutton
                        medRg.check(medRg.getChildAt(position).getId());
                        viewHolder2.medfotRg.check(viewHolder2.medfotRg.getChildAt(position).getId());
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
                viewHolder2.medfotVp.setAdapter(new FragmentPagerAdapter(supportFragmentManager) {
                    @Override
                    public int getCount() {
                        return fragments.size();
                    }

                    @Override
                    public Fragment getItem(int position) {
                        return fragments.get(position);
                    }
                });
                break;
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    @Override
    public void onClick(View v) {

    }

    public void addCarouselList(CarouselBean carouselBean) {
//        data = carouselBean.getData();
        data.add(carouselBean.getData().get(0));
        data.add(carouselBean.getData().get(0));
        data.add(carouselBean.getData().get(0));
        data.add(carouselBean.getData().get(0));
        imgSrc.clear();
//        for (int i = 0; i < data.size(); i++) {
        imgSrc.add(data.get(0).getImgSrc());
        imgSrc.add(data.get(0).getImgSrc());
        imgSrc.add(data.get(0).getImgSrc());
        imgSrc.add(data.get(0).getImgSrc());
//        }
    }

    public void addUserCategoryList(UserCategoryBean userCategoryBean) {
        medCarRvAdapter.add(userCategoryBean);
        medCarRvAdapter.notifyDataSetChanged();
    }

    public void addTop10List(Top10Bean top10Bean) {
        medTopRvAdapter.add(top10Bean);
        medTopRvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClassifyClick(int type) {

    }

    @Override
    public void onItemClick(String uid, String id) {

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.medcar_ima1)
        ImageView medcarIma1;
        @BindView(R.id.medcar_ima2)
        ImageView medcarIma2;
        @BindView(R.id.medcar_ban)
        Banner medcarBan;
        @BindView(R.id.medcar_vf)
        ViewFlipper medcarVf;
        @BindView(R.id.medcar_rv)
        RecyclerView medcarRv;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.medtop_rv)
        RecyclerView medtopRv;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.medfot_pop)
        RadioButton medfotPop;
        @BindView(R.id.medfot_near)
        RadioButton medfotNear;
        @BindView(R.id.medfot_cir)
        RadioButton medfotCir;
        @BindView(R.id.medfot_rg)
        RadioGroup medfotRg;
        @BindView(R.id.medfot_vp)
        ViewPager medfotVp;

        ViewHolder2(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
